﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using Persistence.Dao;
using Domain;
using Persistence;
namespace web.manage
{
    public partial class admin_operate : adminBase
    {
        int id = DNTRequest.GetQueryInt("id", 0);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (id > 0)
                {
                    Administrators entity_Administrators = Singleton<AdministratorsDao>.Instance.GetModel(id);
                    if (entity_Administrators != null)
                    {
                        ViewState["entity_Administrators"] = entity_Administrators;
                        txt_Administrators_username.Value = entity_Administrators.username;
                        txt_Administrators_password.Value = entity_Administrators.password;
                        ddlrole.SelectedValue = entity_Administrators.roles;
                    }
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Administrators entity_Administrators = new Administrators();
            if (id > 0 && ViewState["entity_Administrators"] != null)
            {
                entity_Administrators = (Administrators)ViewState["entity_Administrators"];
            }
            entity_Administrators.username = txt_Administrators_username.Value.Trim();
            entity_Administrators.password = txt_Administrators_password.Value.Trim();
            entity_Administrators.roles = ddlrole.SelectedValue;
            bool bResult = false;
            if (id > 0)
            {
                bResult = Singleton<AdministratorsDao>.Instance.Update(entity_Administrators);
            }
            else
            {
                try
                {
                    if (Singleton<AdministratorsDao>.Instance.GetCount("username='" + entity_Administrators.username + "'") == 0)
                    {
                        Singleton<AdministratorsDao>.Instance.Insert(entity_Administrators);
                        bResult = true;
                    }
                }
                catch { }
            }
            if (bResult)
            {
                Message.alert("操作成功");
                txt_Administrators_username.Value = "";
                txt_Administrators_password.Value = "";
            }
            else
            {
                Message.alert("操作失败，请检查您的数据");
            }
        }
    }
}
